package com.android.execucaocampo;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;

import com.android.beans.Material;
import com.android.beans.TipoMaterial;
import com.android.data.RepositorioMaterial;
import com.android.data.RepositorioTipoMaterial;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class MaterialActivity extends Activity {

	public ListView lista;
	
	EditText edQuantidade;
	
	ArrayList<TipoMaterial> arrayTipoMaterial;	
	ArrayList<Material> arrayMaterial;
	
	RepositorioMaterial repMaterial;
	Material materialAuxiliar = null;
	
	DecimalFormat df = new DecimalFormat("#,###.00");
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.material);
		
		edQuantidade = (EditText) findViewById(R.campo.quantidade);
		
		RepositorioTipoMaterial repTipoMaterial = new RepositorioTipoMaterial(getApplication());
		arrayTipoMaterial = repTipoMaterial.listar("");
		
		final Spinner s2 = (Spinner)findViewById(R.drop.material);
        ArrayAdapter<TipoMaterial> adapter2 = new ArrayAdapter<TipoMaterial>(this, android.R.layout.simple_spinner_item, arrayTipoMaterial);
        adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s2.setAdapter(adapter2);
		
        repMaterial = new RepositorioMaterial(getApplicationContext());			
		arrayMaterial = repMaterial.listar(" ORDER BY descricao");
        
		ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
		HashMap<String, String> map;
		
		for (Material material : arrayMaterial) {
			map = new HashMap<String, String>();
			
			map.put("desc", material.getDescricaoTipoMaterial());
			map.put("unid", material.getUnidadeTipoMaterial());
			map.put("quant", String.valueOf(material.getQuantidade()));							
		
			mylist.add(map);
		}
		
		lista = (ListView) findViewById(R.id.listamateriais);
		
		lista.setAdapter(new SimpleAdapter(this, mylist, R.layout.rowmaterial,
	            new String[] {"desc", "unid", "quant"}, 
	            new int[] {R.id.desc, R.id.unid, R.id.quant}));
		
		Button btnInserir = (Button) findViewById(R.botao.inserir);
		btnInserir.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				if (!s2.getSelectedItem().toString().equals("")) {
					//RepositorioMaterial repmaterial = new RepositorioMaterial(getApplicationContext());
				
					Material material = new Material();
					material.setCodigoOrdemServico(Integer.parseInt(AbaOrdemServicoActivity.numOS));
					
					TipoMaterial tipomaterial = (TipoMaterial) s2.getSelectedItem();
					material.setCodigoTipoMaterial(tipomaterial.getCodigo());
					material.setQuantidade(Double.parseDouble(edQuantidade.getText().toString()));
					
					repMaterial.inserir(material);
					
					repMaterial = new RepositorioMaterial(getApplicationContext());			
					arrayMaterial = repMaterial.listar(" ORDER BY descricao");
					
					ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
					HashMap<String, String> map;
					
					for (Material materiais : arrayMaterial) {
						map = new HashMap<String, String>();
						
						map.put("desc", materiais.getDescricaoTipoMaterial());
						map.put("unid", materiais.getUnidadeTipoMaterial());
						map.put("quant", String.valueOf(materiais.getQuantidade()));		
					
						mylist.add(map);
					}
					
					lista.setAdapter(new SimpleAdapter(getApplicationContext(), mylist, R.layout.rowmaterial,
				            new String[] {"desc", "unid", "quant"}, 
				            new int[] {R.id.desc, R.id.unid, R.id.quant}));								
				} else {
					Toast.makeText(MaterialActivity.this, "Falta informar o material!!!", Toast.LENGTH_LONG).show();
				}				
			}
		});	
	}	
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return false;
	}
}
